<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/taglibs.jsp" %>
<c:set var="managerUserId" value="${param.managerUserId}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>用户注册</title>
    <link rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/OrgService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/UserService.js"></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<table>
    <tr>
        <td width="60">
            <img width="50" height="50" src="${ctx}/images/logo.png"/>
        </td>
        <td valign="bottom" style="color:#C81B0A; vertical-align: bottom">
            <h4>红魔方数据</h4>
        </td>
    </tr>
</table>
<form id="userForm" name="userForm" method="post" onsubmit="return selectUser()">
    <input type="hidden" id="userPoint" name="userPoint" value="100"/>
    <input type="hidden" id="userState" name="userState" value="1"/>
    <input type="hidden" id="managerUserId" name="managerUserId" value="<c:if test='${empty managerUserId}'>54CCAC4A908A60BC6481E73BA50411F2</c:if><c:if test='${!empty managerUserId}'>${managerUserId}</c:if>"/>
    <input type="hidden" id="orgId" name="orgId" placeholder="机构" />
    <table cellspacing="5" cellpadding="5">
        <tr>
            <td colspan="3">
                <input type="tel" id="userMobile" name="userMobile" minlength="11" maxlength="11" placeholder="请准确核对手机号" required="true"/>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="text" id="userName" name="userName" placeholder="姓名" required="true"/>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="search" id="orgFullName" name="orgFullName" placeholder="请搜索选择机构" autocomplete="off" oninput="selectOrgList(this)" required="true"/>
            </td>
        </tr>
        <tr>
            <!--td>
                <input type="tel" id="userVertifyCode" name="userVertifyCode" maxlength="4" placeholder="验证码" required="true"/>
            </td>
            <td>
                <button type="button" id="sendCodeButton" onclick="selectUser()">发送验证码</button>
            </td-->
            <td>
                <button type="submit">申请注册</button>
                <button type="reset">&nbsp;&nbsp;清&nbsp;&nbsp;&nbsp;空&nbsp;&nbsp;</button>
            </td>
        </tr>
    </table>
    <div id="searchDiv" style="width: 93%; border: 1px solid #0099FF; background: white; display: none; position: absolute; z-index: 13;" onmouseleave="closeDiv('searchDiv')">
        <table id="searchTable" class="xTbody" style="width: 100%;">
        </table>
    </div>
</form>
</body>
</html>
<script type="text/javascript">
    var clock = '';
    var nums = 60;
    var btn;
    function selectOrgList(input) {
        dwr.util.setValue("orgId", null);
        $("searchDiv").style.display = "none";
        if (input.value.length > 0) {
            var org = new Org();
            org.orgFullName = input.value;
            org.pageIndex = 0;
            org.pageSize = 10;
            OrgService.queryCrmOrg(org, function (list) {
                if (list.length > 0) {
                    $("searchDiv").style.display = "block";
                    $("searchDiv").style.left = getLeft(input) + "px";
                    $("searchDiv").style.top = getTop(input) + 35 + "px";
                    var table = $("searchTable");
                    while (table.rows.length > 0) {
                        table.deleteRow(0);
                    }
                    var row;
                    var cell;
                    for (var i = 0; i < list.length; i++) {
                        org = list[i];
                        row = table.insertRow();
                        row.style.lineHeight = "30px";
                        row.org = org;
                        cell = row.insertCell();
                        cell.style.textAlign = "left";
                        cell.innerHTML = "<a>" + org.orgFullName + "</a>";
                        cell.onclick = function () {
                            var org = this.parentElement.org;
                            dwr.util.setValue("orgId", org.orgId);
                            dwr.util.setValue("orgFullName", org.orgFullName);
                            closeDiv("searchDiv");
                        }
                    }
                }
            });
        }
    }
    function closeDiv(divId) {
        $(divId).style.display = "none";
    }
    function selectUser() {
        var user = new User();
        user.userMobile = userMobile;
        UserService.selectUserList(user, function (list) {
            if (list.length > 1) {
                loading("该电话号码已存在");
                return false;
            }
            var user = dwr.util.getFormValues("userForm");
            if (!user.orgId) {
                user.orgId = "8a8a802c392cf27301392de718ac0098";
            }
            UserService.insertUser(user, function (userId) {
                var declarerManager = new DeclarerManager();
                declarerManager.declareUserId = userId;
                declarerManager.manageUserId = dwr.util.getValue("managerUserId");
                UserService.insertDeclarerManager(declarerManager, function () {
                    var redirectUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb260cb11fcdb2b4b&redirect_uri=https://www.dazongpai.com/am/controller/wx/token&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
                    UserService.sendWxTemplateMessageByUserIds(dwr.util.getValue("managerUserId"), redirectUrl, "有新用户注册待审核", "待审核", dwr.util.getValue("orgFullName") + dwr.util.getValue("userName") + dwr.util.getValue("userMobile"), "待审核", function () {
                        loading("注册申请已提交，请等候客户经理联系您");
                    });
                });
            });
        });
        return false;
    }
    function verifyCode() {
        var userMobile = dwr.util.getValue("userMobile");
        var userVertifyCode = dwr.util.getValue("userVertifyCode");
        UserService.verifyCode(userMobile, userVertifyCode, function (flag) {
            if (flag) {
                var user = dwr.util.getFormValues("userForm");

                var orgType = dwr.util.getValue("orgType");
                if (!user.orgId) {
                    loading("请选择机构");
                    return false;
                }
                if (!user.userName) {
                    loading("请填写姓名");
                    return false;
                }
                UserService.insertUser(user, orgType, center);
            } else {
                loading("验证码填写错误");
            }
        });
    }
    function sendCode(list) {
        if (list.length == 1) {
            loading("该电话号码已存在");
            return false;
        }
        var userMobile = dwr.util.getValue("userMobile");
        if (nums == 60) {
            btn = $("sendCodeButton");
            btn.disabled = true; //将按钮置为不可点击
            UserService.sendCode(userMobile);
            btn.innerHTML = nums;
            clock = setInterval(doLoop, 1000); //一秒执行一次
        }
    }
    function doLoop() {
        nums--;
        if (nums > 0) {
            btn.innerHTML = nums;
        } else {
            clearInterval(clock); //清除js定时器
            btn.disabled = false;
            btn.innerHTML = '发送短信验证码';
            nums = 60; //重置时间
        }
    }
    function center(userId) {
        window.location = "${ctx}/user/center_user.jsp?userId=" + userId;
    }
</script>